package com.haihui.platform.controller.dust;

import com.haihui.platform.controller.BaseController;
import com.haihui.platform.entity.*;
import com.haihui.platform.service.dead.DeadBaseInfoService;
import com.haihui.platform.service.dust.DustService;
import com.haihui.platform.utils.Common;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by HaiHui on 2017/10/13.
 */
@Controller
@RequestMapping(value="/dust")
public class DustController extends BaseController {
    private static final Logger LOG = Logger.getLogger(DustController.class);
    @Autowired
    private DustService dustService;
    @Autowired
    private DeadBaseInfoService deadBaseInfoService;

    /**
     * 画面list用
     * @param selectModel
     */
    @RequestMapping(value="/list")
    public  void list(DustBaseModel selectModel){
        try{
            PagingBean pagingBean = getPagingBean();
            int count = dustService.getCount(selectModel);
            List<DustBaseModel> list = dustService.list(selectModel,pagingBean);
            this.responseWriter(this.getCommonJsonPage(count,list));

        }catch (Exception e){
            e.printStackTrace();
        }

    }

    /**
     * 添加用
     * @param model
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public void add(@RequestBody DustBaseModel model){
        try{
            //SysAdUserInfo login = (SysAdUserInfo) session.getAttribute("user");
            model.setCreateor("111");
            model.setCreateTime(new Date());
            int result = dustService.add(model);
            if(result > 0){
                this.responseWriter(this.getCommonJsonString(true,"添加成功"));
            }else{
                this.responseWriter(this.getCommonJsonString(false,"添加失败"));
            }

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * update
     * @param model
     */
    @RequestMapping(value = "/update")
    public void update(@RequestBody DustBaseModel model){
        try{
            //SysAdUserInfo login = (SysAdUserInfo) session.getAttribute("user");
            model.setModifyor("111");
            model.setModifyTime(new Date());
            int result = dustService.update(model);
            if(result > 0){
                this.responseWriter(this.getCommonJsonString(true,"更新成功"));
            }else{
                this.responseWriter(this.getCommonJsonString(false,"更新失败"));
            }

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 删除用
     * @param id
     */
    @RequestMapping(value = "/delete")
    public void delete(Long id){
        try{
            //SysAdUserInfo login = (SysAdUserInfo) session.getAttribute("user");
            //是否还有区域，如果有的话不能删除
            Region selectModel =  new Region();
            selectModel.setDustId(id);
            List<Region> list = dustService.selectRegions(selectModel);
            if(list.size() > 0){
                this.responseWriter(this.getCommonJsonString(false,"区数据存在不能删除!"));
            }else{
                DustBaseModel model =  new DustBaseModel();
                model.setModifyor("11");
                model.setModifyTime(new Date());
                model.setDustId(id);
                model.setDelFlg("1");
                int result = dustService.update(model);
                if(result > 0){
                    this.responseWriter(this.getCommonJsonString(true,"删除成功"));
                }else{
                    this.responseWriter(this.getCommonJsonString(false,"删除失败"));
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * detail
     * @param id
     */
    @RequestMapping(value = "/detail")
    public void detail(Long id){
        try{
            DustBaseModel model =  new DustBaseModel();
            model.setDustId(id);
            DustBaseModel result = dustService.detail(model);
            if(result != null){
                this.responseWriter(this.getCommonJsonString(true,"删除成功",result));
            }else{
                this.responseWriter(this.getCommonJsonString(false,"删除失败"));
            }

        }catch (Exception e){
            e.printStackTrace();
        }
    }
    @RequestMapping(value = "/addRegion")
    public void addRegion(Region region){
        try{
            //SysAdUserInfo login = (SysAdUserInfo) session.getAttribute("user");
            region.setCreateor("111");
            region.setCreateDate(new Date());
            int result = dustService.addRegion(region);
            if(result > 0){
                this.responseWriter(this.getCommonJsonString(true,"添加成功"));
            }else{
                this.responseWriter(this.getCommonJsonString(false,"添加失败"));
            }

        }catch (Exception e){
            e.printStackTrace();
        }
    }
    @RequestMapping(value = "/addRow")
    public void addRow(RowModel rowModel){
        try{
            //SysAdUserInfo login = (SysAdUserInfo) session.getAttribute("user");
            rowModel.setCreateor("111");
            rowModel.setLoginDate(new Date());
            int result = dustService.addRow(rowModel);
            if(result > 0){
                this.responseWriter(this.getCommonJsonString(true,"添加成功"));
            }else{
                this.responseWriter(this.getCommonJsonString(false,"添加失败"));
            }

        }catch (Exception e){
            e.printStackTrace();
        }
    }
    @RequestMapping(value = "/addGrave")
    public void addGrave(GraveModel graveModel,Long dustId){
        try{
            //SysAdUserInfo login = (SysAdUserInfo) session.getAttribute("user");
            graveModel.setCreateor("111");
            graveModel.setLoginDate(new Date());
            int result = dustService.addGrave(graveModel);
            //公墓的空穴数+1，总数+1
            DustBaseModel model = new DustBaseModel();
            model.setDustId(dustId);
            model.setModifyTime(new Date());
            model.setModifyor("111");
            int updateCount = dustService.updateDustByGrave(model);

            if(result > 0){
                this.responseWriter(this.getCommonJsonString(true,"添加成功"));
            }else{
                this.responseWriter(this.getCommonJsonString(false,"添加失败"));
            }

        }catch (Exception e){
            e.printStackTrace();
        }
    }
    /**
     * 删除用
     * @param id
     */
    @RequestMapping(value = "/deleteRegion")
    public void deleteRegion(Long id){
        try{
            //查询排数据是否有，有的话无法删除
            RowModel selectModel =  new RowModel();
            selectModel.setRegionId(id);
            List<RowModel> rowList = dustService.selectRows(selectModel);
            if(rowList.size() > 0){
                this.responseWriter(this.getCommonJsonString(false,"排数据存在无法删除!"));
            }else{
                Region model =  new Region();
                //SysAdUserInfo login = (SysAdUserInfo) session.getAttribute("user");
                model.setModifyor("11");
                model.setUpdateDate(new Date());
                model.setRegionId(id);
                int result = dustService.deleteRegion(model);
                if(result > 0){
                    this.responseWriter(this.getCommonJsonString(true,"删除成功"));
                }else{
                    this.responseWriter(this.getCommonJsonString(false,"删除失败"));
                }
            }

        }catch (Exception e){
            e.printStackTrace();
        }
    }
    /**
     * 删除用
     * @param id
     */
    @RequestMapping(value = "/deleteRow")
    public void deleteRow(Long id){
        try{
            //查询墓穴数据是否有，有的话无法删除
            GraveModel selectModel =  new GraveModel();
            selectModel.setRowId(id);
            List<GraveModel> list = dustService.selectGraves(selectModel);
            if(list.size() > 0){
                this.responseWriter(this.getCommonJsonString(false,"墓穴数据存在无法删除!"));
            }else{
                RowModel model =  new RowModel();
                //SysAdUserInfo login = (SysAdUserInfo) session.getAttribute("user");
                model.setModifyor("11");
                model.setUpdateDate(new Date());
                model.setRowId(id);
                int result = dustService.deleteRow(model);
                if(result > 0){
                    this.responseWriter(this.getCommonJsonString(true,"删除成功"));
                }else{
                    this.responseWriter(this.getCommonJsonString(false,"删除失败"));
                }
            }

        }catch (Exception e){
            e.printStackTrace();
        }
    }
    /**
     * 删除用
     * @param id
     */
    @RequestMapping(value = "/deleteGrave")
    public void deleteGrave(Long id,Long dustId){
        try{
            //查询墓穴里是否有人，有人的话无法删除
            DeadBaseInfo baseInfo = new DeadBaseInfo();
            baseInfo.setBurySite(id);
            if(deadBaseInfoService.count(baseInfo) > 0){
                this.responseWriter(this.getCommonJsonString(false,"墓穴里存在逝者,无法删除!"));
            }else{
                GraveModel model =  new GraveModel();
                //SysAdUserInfo login = (SysAdUserInfo) session.getAttribute("user");
                model.setModifyor("11");
                model.setUpdateDate(new Date());
                model.setGraveId(id);
                int result = dustService.deleteGrave(model);
                //删除时候公墓总数-1，空穴-1
                DustBaseModel dustmodel = new DustBaseModel();
                dustmodel.setDustId(dustId);
                dustmodel.setModifyTime(new Date());
                dustmodel.setModifyor("111");
                int updateCount = dustService.deleteDustByGrave(dustmodel);
                if(result > 0){
                    this.responseWriter(this.getCommonJsonString(true,"删除成功"));
                }else{
                    this.responseWriter(this.getCommonJsonString(false,"删除失败"));
                }
            }



        }catch (Exception e){
            e.printStackTrace();
        }
    }
    /**
     * update
     * @param model
     */
    @RequestMapping(value = "/updateRegion")
    public void updateRegion(@RequestBody Region model){
        try{
            //SysAdUserInfo login = (SysAdUserInfo) session.getAttribute("user");
            model.setModifyor("111");
            model.setUpdateDate(new Date());
            int result = dustService.updateRegion(model);
            if(result > 0){
                this.responseWriter(this.getCommonJsonString(true,"更新成功"));
            }else{
                this.responseWriter(this.getCommonJsonString(false,"更新失败"));
            }

        }catch (Exception e){
            e.printStackTrace();
        }
    }
    /**
     * update
     * @param model
     */
    @RequestMapping(value = "/updateRow")
    public void updateRow(@RequestBody RowModel model){
        try{
            //SysAdUserInfo login = (SysAdUserInfo) session.getAttribute("user");
            model.setModifyor("111");
            model.setUpdateDate(new Date());
            int result = dustService.updateRow(model);
            if(result > 0){
                this.responseWriter(this.getCommonJsonString(true,"更新成功"));
            }else{
                this.responseWriter(this.getCommonJsonString(false,"更新失败"));
            }

        }catch (Exception e){
            e.printStackTrace();
        }
    }
    /**
     * update
     * @param model
     */
    @RequestMapping(value = "/updateGrave")
    public void updateGrave(@RequestBody GraveModel model){
        try{
            //SysAdUserInfo login = (SysAdUserInfo) session.getAttribute("user");
            model.setModifyor("111");
            model.setUpdateDate(new Date());
            int result = dustService.updateGrave(model);
            if(result > 0){
                this.responseWriter(this.getCommonJsonString(true,"更新成功"));
            }else{
                this.responseWriter(this.getCommonJsonString(false,"更新失败"));
            }

        }catch (Exception e){
            e.printStackTrace();
        }
    }
    /**
     * 区list用
     * @param id
     */
    @RequestMapping(value="/selectRegions")
    public  void selectRegions(Long id){
        try{
            Region selectModel =  new Region();
            selectModel.setDustId(id);
            List<Region> list = dustService.selectRegions(selectModel);
            this.responseWriter(this.getCommonJsonString(true,"查询成功",list));
        }catch (Exception e){
            e.printStackTrace();
        }

    }
    /**
     * 排list用
     * @param id
     */
    @RequestMapping(value="/selectRows")
    public  void selectRows(Long id){
        try{
            Map<String,Object> result= new HashMap();
            RowModel selectModel =  new RowModel();
            selectModel.setRegionId(id);
            List<RowModel> rowList = dustService.selectRows(selectModel);
            result.put("rowList",rowList);
            if(rowList.size() > 0) {
                Long rowId = ((RowModel) rowList.get(0)).getRowId();
                String rowName = ((RowModel) rowList.get(0)).getRowName();
                result.put("rowName", rowName);
                result.put("rowId", rowId);
                GraveModel graveModel = new GraveModel();
                graveModel.setRowId(rowId);
                List<GraveModel> graveList = dustService.selectGraves(graveModel);
                result.put("graveList", graveList);
            }
            this.responseWriter(this.getCommonJsonString(true,"查询成功",result));


        }catch (Exception e){
            e.printStackTrace();
        }

    }
    /**
     * 墓穴list用
     * @param id
     */
    @RequestMapping(value="/selectGraves")
    public  void selectGraves(Long id){
        try{
            GraveModel selectModel =  new GraveModel();
            selectModel.setRowId(id);
            List<GraveModel> list = dustService.selectGraves(selectModel);
            this.responseWriter(this.getCommonJsonString(true,"查询成功",list));
        }catch (Exception e){
            e.printStackTrace();
        }

    }
    /**
     * 墓穴页面的初期化
     */
    @RequestMapping(value = "graveInit")
    public void graveInit(Long id){
        try{
            Map<String,Object> result= new HashMap();
            //根据公墓id查出所有区
            Region selectModel =  new Region();
            selectModel.setDustId(id);
            List<Region> list = dustService.selectRegions(selectModel);
            result.put("regionList",list);
            if(list.size() > 0){
                Long regionId = ((Region)list.get(0)).getRegionId();
                String regionName = ((Region)list.get(0)).getRegionName();
                result.put("regionName",regionName);
                result.put("regionId",regionId);

                RowModel rowModel =  new RowModel();
                rowModel.setRegionId(regionId);
                List<RowModel> rowList = dustService.selectRows(rowModel);
                result.put("rowList",rowList);
                if(rowList.size() > 0){
                    Long rowId = ((RowModel)rowList.get(0)).getRowId();
                    String  rowName = ((RowModel)rowList.get(0)).getRowName();
                    result.put("rowName",rowName);
                    result.put("rowId",rowId);
                    GraveModel graveModel =  new GraveModel();
                    graveModel.setRowId(rowId);
                    List<GraveModel> graveList = dustService.selectGraves(graveModel);
                    result.put("graveList",graveList);

                }

                this.responseWriter(this.getCommonJsonString(true,"查询成功",result));
            }else{
                this.responseWriter(this.getCommonJsonString(false,"没有相关数据，请先添加区"));
            }




        }catch (Exception e){
            e.printStackTrace();
        }


    }
    @RequestMapping("setGraveDetail")
    public void setGraveDetail(Long id){
        try{
            Map<String,Object> result =  new HashMap<String, Object>();
            //根据id查询逝者基本信息
            DeadBaseInfo deadBaseInfo = new DeadBaseInfo();
            deadBaseInfo.setBaseId(id);
            DeadBaseInfo deadInfo = deadBaseInfoService.detail(deadBaseInfo);
            result.put("deadInfo",deadInfo);
            if(deadBaseInfo != null){
                Region selectRegion  = new Region();
                selectRegion.setDustId(deadInfo.getDustId());
                List<Region> region = dustService.selectRegions(selectRegion);
                result.put("region",region);
                this.responseWriter(this.getCommonJsonString(true,"查询成功",result));

            }else{
                this.responseWriter(this.getCommonJsonString(false,"查询失败"));

            }



        }catch (Exception e){
            e.printStackTrace();
        }

    }
    @RequestMapping("setGrave")
    public void setGrave(Long id){
        try{
            GraveModel graveModel = new GraveModel();
            graveModel.setRowId(id);
            graveModel.setUseFlg("0");
            List<GraveModel> graveList = dustService.selectGraves(graveModel);
            if(graveList.size() > 0){
                this.responseWriter(this.getCommonJsonString(true,"系统分配成功",graveList.get(0)));
            }else{
                this.responseWriter(this.getCommonJsonString(false,"请选择其他排，该排已满!"));

            }


        }catch (Exception e){
            e.printStackTrace();
        }

    }
    @RequestMapping("saveGraveInfo")
    public void saveGraveInfo(Long graveId,String graveName,Long dustId,Long baseId,Long secondBaseId,String type){
        try{
            //逝者信息的墓穴更新
            DeadBaseInfo baseInfo = new DeadBaseInfo();
            baseInfo.setBaseId(baseId);
            baseInfo.setModifyor("111");
            baseInfo.setModifyTime(new Date());
            baseInfo.setBurySite(graveId);
            baseInfo.setBurySiteName(graveName);
            baseInfo.setDeadStatus(Common.dead_status_3);
            if(type.equals(Common.type_1)){
                //初葬的话逝者信息修改
                baseInfo.setBuryMode(Common.type_1);

            }else{
                //合葬的话逝者信息为合葬
                baseInfo.setBuryMode(Common.type_2);

            }
            deadBaseInfoService.update(baseInfo);

            //墓穴的使用flg更新
            GraveModel graveModel = new GraveModel();
            graveModel.setGraveId(graveId);
            graveModel.setUseFlg(Common.use_flg_1);
            graveModel.setModifyor("111");
            graveModel.setUpdateDate(new Date());
            dustService.updateGrave(graveModel);
            DustBaseModel baseModel = new DustBaseModel();
            baseModel.setModifyor("111");
            baseModel.setModifyTime(new Date());
            baseModel.setDustId(dustId);
            if(type.equals(Common.type_1)){
                // 初葬的话 公墓的空穴数-1，已葬人数+1
                dustService.updateByFirstGrave(baseModel);
            }else{
                //合葬的话 已葬人数+1
                dustService.updateBySecondGrave(baseModel);
                //选择人的方式也修改为合葬
                DeadBaseInfo secondBaseInfo = new DeadBaseInfo();
                secondBaseInfo.setBaseId(secondBaseId);
                secondBaseInfo.setBuryMode(Common.type_2);
                secondBaseInfo.setModifyor("111");
                secondBaseInfo.setModifyTime(new Date());
                deadBaseInfoService.update(secondBaseInfo);

            }

            this.responseWriter(this.getCommonJsonString(true,"保存成功"));

        }catch (Exception e){
            e.printStackTrace();

            this.responseWriter(this.getCommonJsonString(false,"保存失败"));
        }


    }
    @RequestMapping("deadInfoByGrave")
    public void deadInfoByGrave(DeadBaseInfo baseInfo){
        try{
            SysAdUserInfo login = (SysAdUserInfo) session.getAttribute("user");
            //baseInfo.setDustId(login.getDustId());
            baseInfo.setDustId(23L);
            int count = deadBaseInfoService.count(baseInfo);
            List<DeadBaseInfo> list = deadBaseInfoService.list(baseInfo);
            this.responseWriter(this.getCommonJsonPage(count,list));
        }catch(Exception e){
            LOG.error(e.getMessage(),e);
            this.responseWriter(this.getCommonJsonString(false, "查询失败"));
        }

    }



}
